import { router_param_metadata } from "../constant";
import { RouteParamTypes } from "../enum";
import { RouteParam } from "./types";

const createParamMapping = (type: RouteParamTypes) => {
  return (id?: string) => {
    return (target: any, key: string, index: any) => {
      let routerParamData: RouteParam[] = [];
      if (Reflect.hasOwnMetadata(router_param_metadata, target.constructor)) {
        routerParamData = Reflect.getMetadata(router_param_metadata, target.constructor);
      }
      routerParamData.push({
        paramtype: type || "query", // 类型
        data: id, // 参数
        methodName: key,
        index,
        target: target.constructor,
      });
      Reflect.defineMetadata(router_param_metadata, routerParamData, target.constructor);
    };
  };
};
// ParameterDecorator
// constructor
export const Query = (id?: string): any => {
  return createParamMapping(RouteParamTypes.QUERY)(id);
};
export const Body = (id?: string) => {
  return createParamMapping(RouteParamTypes.BODY)(id);
};
export const Param = (id?: string) => {
  return createParamMapping(RouteParamTypes.PARAM)(id);
};
export const Headers = (id?: string) => {
  return createParamMapping(RouteParamTypes.HEADERS)(id);
};
export const Session = (id?: string) => {
  return createParamMapping(RouteParamTypes.SESSION)(id);
};
export const RequestIP = (id?: string) => {
  return createParamMapping(RouteParamTypes.REQUEST_IP)(id);
};
